package com.gxuwz.attend.web.servlet;

import com.gxuwz.attend.context.AppContext;
import com.gxuwz.attend.service.LoginService;
import com.gxuwz.attend.tools.MD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 基于页面控制器模式的登录验证
 */
@WebServlet(name = "loginServlet", urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    private final static String VIEW_ADMIN = "adminIndex.jsp";
    private final static String VIEW_INSTRUCTOR = "instructorIndex.jsp";
    private final static String VIEW_STUDENT = "studentIndex.jsp";
    private final static String VIEW_LOGIN = "login.jsp";
    // 业务组件
    private LoginService userService;

    @Override
    public void init() throws ServletException {
        super.init();
        // 从IoC容器获取实例化bean组件
        userService = AppContext.getBean(LoginService.class);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");

        // 对输入密码进行 MD5 加密
        String encryptedPassword = MD5Util.encryptMd5(password);

        // 验证登录
        Object userObject = userService.login(username, password, role);
        if (userObject != null) {
            System.out.println("登录成功");

            // 将用户对象存入会话
            HttpSession session = request.getSession(true); // 没有会话就创建会话
            session.setAttribute("currentUser", userObject); // 统一使用 currentUser 来存储用户对象
            session.setAttribute("userRole", role); // 存储角色信息
            // 验证会话中是否存入成功
            Object storedUserObject = session.getAttribute("currentUser");
            if (storedUserObject != null) {
                System.out.println("用户对象成功存入会话: " + storedUserObject);
            } else {
                System.out.println("用户对象未成功存入会话");
            }
switch (role) {
    case "admin":
    // 跳转到成功页面
    response.sendRedirect(VIEW_ADMIN);
    break;
    case "instructor":
    // 跳转到成功页面
    response.sendRedirect(VIEW_INSTRUCTOR);
    break;
    case "student":
    // 跳转到成功页面
    response.sendRedirect(VIEW_STUDENT);
    break;
    default:
}
        } else {
            System.out.println("登录失败");
            String errorMessage = "用户名或密码错误";
            String encodedMessage = URLEncoder.encode(errorMessage, StandardCharsets.UTF_8.toString());
            response.sendRedirect(VIEW_LOGIN+"?error=" + encodedMessage);  // 跳转回登录页面并传递错误信息

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

}
